﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using TestsModel.Enums;

namespace TestContentTool.TestControls.RadioTest
{
    public partial class RadioTestControl : UserControl, IIdentifiableTestControl
    {
        private string id = Guid.NewGuid().ToString();
        public string Id { get { return id; } }
        public TestTypes TestType { get { return TestTypes.RadioTest; } }
        public string Display { get { return radioTestQestionTextBox.Text; } }
        public BindingList<RadioStatement> Statements { get; private set; }
        public int CorrectIndex { get; private set; }

        private TestsModel.Data.RadioTest radioTest;

        public RadioTestControl(TestsModel.Data.RadioTest test)
        {
            InitializeComponent();
            if (test == null)
                throw new ArgumentNullException();

            radioTest = test;
            Initialize();
        }

        private void Initialize()
        {
            Statements = new BindingList<RadioStatement>();
            foreach (var statement in radioTest.Statements)
            {
                Statements.Add(new RadioStatement(statement.Value));
            }
            CorrectIndex = radioTest.Answer;

            radioTestQestionTextBox.Text = radioTest.QuestionText;
            radioTestItemsGrid.DataSource = Statements;
        }

        private void radioTestAddButton_Click(object sender, EventArgs e)
        {
            var textDialog = new TextEditDialog() { Value = string.Empty };
            if (textDialog.ShowDialog(this) == DialogResult.OK && !string.IsNullOrEmpty(textDialog.Value))
            {
                Statements.Add(new RadioStatement(textDialog.Value));
            }
        }

        private void radioTestRemoveButton_Click(object sender, EventArgs e)
        {
            if (radioTestItemsGrid.SelectedRows.Count <= 0)
                return;

            var selectedItem = (RadioStatement)radioTestItemsGrid.SelectedRows[0].DataBoundItem;

            Statements.Remove(selectedItem);
        }

        public TestsModel.Data.RadioTest GetTest()
        {
            var questionStatements = new Dictionary<int, string>();
            for (int i = 0; i < Statements.Count; i++)
            {
                questionStatements.Add(i, Statements[i].Statement);
            }
            return new TestsModel.Data.RadioTest()
            {
                Answer = CorrectIndex,
                QuestionText = radioTestQestionTextBox.Text,
                Statements = questionStatements
            };
        }

        private void radioTestUpButton_Click(object sender, EventArgs e)
        {
            if (radioTestItemsGrid.SelectedRows.Count <= 0)
                return;

            var selectedItem = (RadioStatement)radioTestItemsGrid.SelectedRows[0].DataBoundItem;
            var currentIndex = radioTestItemsGrid.SelectedRows[0].Index;
            if (currentIndex > 0)
            {
                Statements.RemoveAt(currentIndex);
                Statements.Insert(currentIndex - 1, selectedItem);
                radioTestItemsGrid.ClearSelection();
                radioTestItemsGrid.Rows[currentIndex - 1].Selected = true;
            }
        }

        private void radioTestDownButton_Click(object sender, EventArgs e)
        {
            if (radioTestItemsGrid.SelectedRows.Count <= 0)
                return;

            var selectedItem = (RadioStatement)radioTestItemsGrid.SelectedRows[0].DataBoundItem;
            var currentIndex = radioTestItemsGrid.SelectedRows[0].Index;
            if (currentIndex < radioTestItemsGrid.Rows.Count - 1)
            {
                Statements.RemoveAt(currentIndex);
                Statements.Insert(currentIndex + 1, selectedItem);
                radioTestItemsGrid.ClearSelection();
                radioTestItemsGrid.Rows[currentIndex + 1].Selected = true;
            }
        }

        private void radioTestCorrectButton_Click(object sender, EventArgs e)
        {
            if (radioTestItemsGrid.SelectedRows.Count <= 0)
                return;

            var selectedItem = (RadioStatement)radioTestItemsGrid.SelectedRows[0].DataBoundItem;
            CorrectIndex = radioTestItemsGrid.SelectedRows[0].Index;
            correctLabel.Text = string.Format("Id: {0} - {1}", radioTestItemsGrid.SelectedRows[0].Index, selectedItem.Statement);
        }
    }
}
